﻿using Infrastructure;
using Infrastructure.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using MySqlX.XDevAPI.Relational;
using SnowTech.SmartTms.App.Interface;
using SnowTech.SmartTms.App.Line.Request;
using SnowTech.SmartTms.App.Line.Response;
using SnowTech.SmartTms.App.Request;
using SnowTech.SmartTms.App.Response;
using SnowTech.SmartTms.Repository;
using SnowTech.SmartTms.Repository.Domain;
using SnowTech.SmartTms.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SnowTech.SmartTms.App.Line
{
    public class LineApp : BaseStringApp<TmsLine,OpenAuthDBContext>
    {
        private RevelanceManagerApp _revelance;

        public LineApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<TmsLine, OpenAuthDBContext> repository, RevelanceManagerApp app, IAuth auth) : base(unitWork, repository, auth)
        {
            _revelance = app;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        public void Add(LineView obj)  
        {
            UnitWork.ExecuteWithTransaction(() =>
            {
                TmsLine tmsLine = obj;
                UnitWork.Add(tmsLine); 
                UnitWork.Save(); 
                obj.Id = tmsLine.Id;
                obj.LineName = tmsLine.LineName;
                obj.Origin = tmsLine.Origin;
                obj.Destination = tmsLine.Destination;
                obj.IsOutsource = tmsLine.IsOutsource;
                obj.OriginAddress = tmsLine.OriginAddress;
                obj.DestinationAddress = tmsLine.DestinationAddress;
                obj.Shipper = tmsLine.Shipper;
                obj.Phone = tmsLine.Phone;
                obj.CreateTime =DateTime.Now;
                obj.Unit = tmsLine.Unit;
                obj.Remark = tmsLine.Remark;

            });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        public override void Delete(string[] ids)
        {
            UnitWork.ExecuteWithTransaction(() =>
            {
                UnitWork.Delete<Relevance>(u => (u.Key == Define.ROLEMODULE || u.Key == Define.ROLEELEMENT) && ids.Contains(u.FirstId));
                UnitWork.Delete<Relevance>(u => u.Key == Define.USERROLE && ids.Contains(u.SecondId));
                UnitWork.Delete<TmsLine>(u => ids.Contains(u.Id));
                UnitWork.Save();
            });
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TableData Show (QueryLineListReq request)
        {
            var loginContext = _auth.GetCurrentUser();
            if (loginContext == null)
            {
                throw new CommonException("登录已过期", Define.INVALID_TOKEN);
            }


            var result = new TableData();
            var resources = GetDataPrivilege("u");
            if (!string.IsNullOrEmpty(request.LineName))
            {
                resources = resources.Where(u => u.LineName.Contains(request.LineName));
            }
            if (!string.IsNullOrEmpty(request.Origin))
            {
                resources = resources.Where(u => u.Origin.Contains(request.Origin));
            }
            if (!string.IsNullOrEmpty(request.Destination))
            {
                resources = resources.Where(u => u.Destination.Contains(request.Destination));
            }
            if (!string.IsNullOrEmpty(request.Phone))
            {
                resources = resources.Where(u => u.Phone.Contains(request.Phone));
            }
            if (!string.IsNullOrEmpty(request.Unit))
            {
                resources = resources.Where(u => u.Unit.Contains(request.Unit));
            }
            if (request.IsOutsource != 0)
            {
                resources = resources.Where(u => u.IsOutsource==request.IsOutsource);
            }

            result.data = resources.Skip((request.page - 1) * request.limit).Take(request.limit).ToList(); 
            result.count =  resources.Count();
            return result;
        }
        //public List<TmsLine> Load(QueryLineListReq request)
        //{
        //    var loginUser = _auth.GetCurrentUser(); nn      //    var tmsLines = loginUser.TmsLines; 
        //    if (!string.IsNullOrEmpty(request.key))
        //    {
        //        tmsLines = tmsLines.Where(u => u.Linename.Contains(request.key)|| u.Origin.Contains(request.key) || u.Destination.Contains(request.key) || u.Phone.Contains(request.key) || u.Unit.Contains(request.key)).ToList();
        //    }

        //    return tmsLines; 
        //}

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj"></param>
        public void Update(LineView obj)     
        {
            TmsLine tmsLine  = obj;

            UnitWork.Update<TmsLine>(u => u.Id == obj.Id, u => new TmsLine
            {
                LineName = tmsLine.LineName,
                Origin = tmsLine.Origin,
                Destination = tmsLine.Destination,
                IsOutsource = tmsLine.IsOutsource,
                OriginAddress = tmsLine.OriginAddress,
                DestinationAddress = tmsLine.DestinationAddress,
                Shipper = tmsLine.Shipper, 
                Phone = tmsLine.Phone,
                Unit = tmsLine.Unit,
                Remark = tmsLine.Remark,
            });

        }
    }
}
